package com.etime.shuhuiyuan.controllers.role;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.common.BaseMessage;
import com.etime.shuhuiyuan.common.ListMessage;
import com.etime.shuhuiyuan.common.PageMessage;
import com.etime.shuhuiyuan.common.PojoMessage;
import com.etime.shuhuiyuan.pojo.Role;
import com.etime.shuhuiyuan.services.role.interfaces.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * controller控制层
 */
@Api(tags = "role案例接口")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation("添加role案例")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "roleid",value = "自增长主键，不传递"),
            @ApiImplicitParam(name = "rolename",value = "传递字符串"),
            @ApiImplicitParam(name = "roledesc",value = "传递字符串"),
            @ApiImplicitParam(name = "rolestate",value = "状态，1-正常，0-不可用")
    })


    @PostMapping("/insert")
    public BaseMessage insertRole(Role role){
        BaseMessage msg = new BaseMessage();
        int rtn=roleService.insertRole(role);
        msg.setCode(rtn);
        if(rtn>0){
            msg.setMsg("保存role成功");
        }else {
            msg.setMsg("保存role失败");
        }
        return msg;
    }
    @ApiOperation("修改role案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleid",value = "主键，必须传递"),
            @ApiImplicitParam(name = "rolename",value = "传递字符串"),
            @ApiImplicitParam(name = "roledesc",value = "传递字符串"),
            @ApiImplicitParam(name = "rolestate",value = "状态，1-正常，0-不可用")
    })
    @PostMapping("/update")
    public BaseMessage updateRole(Role role){
        BaseMessage msg=new BaseMessage();
        int rtn = roleService.updateRole(role);
        msg.setCode(rtn);
        if(rtn>0){
            msg.setMsg("修改role成功");
        }else {
            msg.setMsg("修改role失败");
        }
        return msg;
    }
    @ApiOperation("查询role列表案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rolename",value = "传递字符串"),
            @ApiImplicitParam(name = "rolestate",value = "状态，1-正常，0-不正常")
    })
    @GetMapping("/list")
    public ListMessage<Role> selectRoleList(Role role){
        ListMessage<Role> listMessage=new ListMessage<>();
        List<Role> list = roleService.selectRole(role);
        if(list!=null){
            listMessage.setDatalist(list);
            listMessage.setCode(1);
        }else {
            listMessage.setCode(0);
            listMessage.setMsg("没有查询到数据");
        }
        return listMessage;
    }
    @ApiOperation("分页查询role案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rolename",value = "传递字符串"),
            @ApiImplicitParam(name = "rolestate",value = "状态，1-正常,0-不正常"),
            @ApiImplicitParam(name = "pagenum",value = "第几页"),
            @ApiImplicitParam(name = "pagesize",value = "每页多少行")

    })
    @GetMapping("/page")
    public PageMessage<Role> selectRoleByPage(Role role,Long pagenum,Long pagesize){
        Page<Role> pagedata=roleService.selectRoleByPage(role,pagenum,pagesize);
        PageMessage<Role> msg=new PageMessage<>();
        if(pagedata!=null){
            msg.setCode(1);
            msg.setPagedata(pagedata);
        }else {
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }
    @ApiOperation("根据id查询role的案例")
    @ApiImplicitParam("roleid")
    @GetMapping("/get/{roleid}")
    public PojoMessage<Role> selectRoleById(@PathVariable("roleid") int roleid){
        PojoMessage<Role> msg=new PojoMessage<>();
        Role role=roleService.selectRoleById(roleid);
        if(role!=null){
            msg.setCode(1);
            msg.setData(role);
        }else {
            msg.setCode(0);
            msg.setMsg("没有找到指定数据");
        }
        return msg;
    }


}